package com.mewp.permissions.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mewp.permissions.common.entity.ResultCodeEnum;
import com.mewp.permissions.common.utils.JwtHelper;
import com.mewp.permissions.model.po.SysUser;
import com.mewp.permissions.model.vo.LoginVo;
import com.mewp.permissions.model.vo.RouterVO;
import com.mewp.permissions.model.vo.SysUserQueryVo;
import com.mewp.permissions.system.execption.CustomerException;
import com.mewp.permissions.system.mapper.SysUserMapper;
import com.mewp.permissions.system.service.SysMenuService;
import com.mewp.permissions.system.service.SysRoleService;
import com.mewp.permissions.system.service.SysUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author mewp
 * @description 针对表【sys_user(用户表)】的数据库操作Service实现
 * @createDate 2023-07-28 10:26:45
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Resource
    private SysMenuService menuService;
    @Resource
    private SysRoleService roleService;

    @Override
    public Page<SysUser> selectPage(Integer page, Integer size, SysUserQueryVo adminQueryVo) {
        Page<SysUser> pageParam = new Page<>(page, size);
        return baseMapper.selectPage(pageParam, adminQueryVo);
    }

    @Override
    public boolean updateStatus(Long id, Integer status) {
        SysUser user = baseMapper.selectById(id);
        if (Objects.isNull(user)) {
            throw new CustomerException(ResultCodeEnum.ACCOUNT_ERROR);
        }
        user.setStatus(status);
        return baseMapper.updateById(user) > 0;
    }

    @Override
    public Map<String, Object> login(LoginVo loginVo) {
        SysUser user = getByUserName(loginVo.getUsername());
        if (Objects.isNull(user)) {
            throw new CustomerException(ResultCodeEnum.ACCOUNT_ERROR);
        }
        if (user.getStatus() == 0) {
            throw new CustomerException(ResultCodeEnum.ACCOUNT_STOP);
        }
        Map<String, Object> map = new HashMap<>();
        String token = JwtHelper.createToken(user.getId(), user.getUsername());
        map.put("token", token);
        return map;
    }

    @Override
    public Map<String, Object> getUserInfo(String token) {
        String userName = JwtHelper.getUserName(token);
        if (!StringUtils.hasText(userName)) {
            throw new CustomerException(ResultCodeEnum.LOGIN_AUTH);
        }
        SysUser user = getByUserName(userName);

        Map<String, Object> map = new HashMap<>();
        List<String> roles = List.of("admin");
        map.put("roles", roles);
        map.put("name", userName);
        map.put("introduction", "I am a super administrator");
        //使用默认头像
        map.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        //菜单权限数据
        List<RouterVO> routerVOS = menuService.getUserMenuList(user.getId());
        map.put("routers", routerVOS);
        //按钮权限数据
        List<String> permsList = menuService.getUserButtonList(user.getId());
        map.put("buttons", permsList);
        return map;
    }

    @Override
    public SysUser getByUserName(String userName) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, userName);
        return baseMapper.selectOne(queryWrapper);
    }
}




